﻿Imports System.Data.OleDb

Public Class PhotoDetails
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then Return
        'if it is a post back, then we jump out of the function and do not run the rest below.'
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("KOPConn").ConnectionString)
        Dim sql = "INSERT INTO PhotoViewedByIDs (Photofkid,Userfkid) VALUES (@f1,@f2)"
        Dim cmd = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f2", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd.ExecuteNonQuery()
        conn.Close()

        lb_Liked.Visible = False
        btn_Dislike.Visible = False

        Dim sql2 = "SELECT * FROM UserLikes WHERE Photofkid = @f1 AND Reader = @f2"
        Dim cmd2 = New OleDbCommand(sql2, conn)
        conn.Open()

        cmd2.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd2.Parameters.AddWithValue("@f2", User.Identity.Name)

        Dim dr = cmd2.ExecuteReader()

        If dr.HasRows Then
            'if userlikes has any data for this picture, show the "you like it"
            lb_Liked.Visible = True
            btn_Dislike.Visible = True
            btn_Like.Visible = False
        Else
            'hide the "you like it" and show the "like" button
            lb_Liked.Visible = False
            btn_Dislike.Visible = False
            btn_Like.Visible = True
        End If

        conn.Close()
    End Sub

    Protected Sub btn_Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Submit.Click
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("KOPConn").ConnectionString)
        Dim sql = "INSERT INTO Comment (Userfkid,Subject,Message,Photofkid,AddDate) VALUES (@f1,@f2,@f3,@f4,@f5)"
        Dim cmd = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd.Parameters.AddWithValue("@f2", tb_Subject.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Message.Text)
        cmd.Parameters.AddWithValue("@f4", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f5", Format(Now(), "dd/MM/yyyy ") & Format(Now(), "HH:mm:ss"))
        cmd.ExecuteNonQuery()
        conn.Close()
        Response.Redirect("PhotoDetails.aspx?ID=" & Request.Params("ID"))
    End Sub

    Protected Sub btn_Select_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Select.Click
        Dim photoID = Request.Params("ID")
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("KOPConn").ConnectionString)
        conn.Open()

        Dim newtags = tb_NewTag.Text.Split(" ")
        For Each newtag As String In newtags
            Dim TagExistCmd As New OleDbCommand("SELECT * FROM Tag WHERE TagName=@f1", conn)
            TagExistCmd.Parameters.AddWithValue("@f1", newtag)
            Dim dr = TagExistCmd.ExecuteReader()
            If Not dr.HasRows Then
                Dim addTagCmd As New OleDbCommand("INSERT INTO Tag (TagName) VALUES (@f1)", conn)
                addTagCmd.Parameters.AddWithValue("@f1", newtag)
                addTagCmd.ExecuteNonQuery()
                Dim findNewTagIDCmd As New OleDbCommand("SELECT ID FROM Tag WHERE TagName=@f1", conn)
                findNewTagIDCmd.Parameters.AddWithValue("@f1", newtag)
                Dim newTagDR = findNewTagIDCmd.ExecuteReader()
                newTagDR.Read()
                Dim newID = newTagDR.Item("ID")
                Dim makeAssociationCmd As New OleDbCommand("INSERT INTO TagJunction (Tagfkid,Photofkid) VALUES (@f1,@f2)", conn)
                makeAssociationCmd.Parameters.AddWithValue("@f1", newID)
                makeAssociationCmd.Parameters.AddWithValue("@f2", photoID)
                makeAssociationCmd.ExecuteNonQuery()
            End If
        Next


        For Each list As ListItem In CheckBoxList1.Items
            If (list.Selected = True) Then
                Dim sql = "INSERT INTO TagJunction (Tagfkid, Photofkid) VALUES (@f1,@f2)"
                Dim cmd As New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@f1", list.Value)
                cmd.Parameters.AddWithValue("@f2", photoID)
                cmd.ExecuteNonQuery()
            End If
        Next
        conn.Close()
        Response.Redirect("PhotoDetails.aspx?ID=" & Request.Params("ID"))
    End Sub

    Protected Sub btn_Like_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Like.Click
        lb_Liked.Visible = True
        btn_Dislike.Visible = True
        btn_Like.Visible = False

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("KOPConn").ConnectionString)
        Dim sql = "INSERT INTO UserLikes (Photofkid,Reader) VALUES (@f1,@f2)"
        Dim cmd = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd.ExecuteNonQuery()
        conn.Close()
    End Sub

    Protected Sub btn_Dislike_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Dislike.Click
        'this is the function to delete the "like" data from the database so that to dislike the picture
        lb_Liked.Visible = False
        btn_Dislike.Visible = False
        btn_Like.Visible = True

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("KOPConn").ConnectionString)
        Dim sql = "DELETE FROM UserLikes WHERE Photofkid = @f1 AND Reader = @f2"
        Dim cmd = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd.ExecuteNonQuery()
        conn.Close()
    End Sub

    Protected Sub btn_Rating_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Rating.Click
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("KOPConn").ConnectionString)

        Dim delPreRatings = "DELETE FROM RateRecord WHERE Photofkid=@f1 AND Userfkid=@f2"
        Dim cmd2 = New OleDbCommand(delPreRatings, conn)
        conn.Open()
        cmd2.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd2.Parameters.AddWithValue("@f2", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd2.ExecuteNonQuery()

        Dim sql = "INSERT INTO RateRecord (Rate,Photofkid,Userfkid) VALUES (@f1,@f2,@f3)"
        Dim cmd = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", Rating1.CurrentRating)
        cmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f3", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd.ExecuteNonQuery()
        conn.Close()
    End Sub
End Class